package ${basePackage}.service${m.prefixPoint(table.modelName)}.impl;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import com.github.pagehelper.PageHelper;
import com.jd.jxt.jxpp.common.response.PageResult;
import com.jd.jxt.jxpp.common.response.PageInfo;
import com.jd.xtl.common.spi.response.APIResult;
import com.jd.xtl.jdyx.cms.common.enums.YesOrNoFlagEnums;
import com.jd.xtl.jdyx.cms.common.exception.BaseException;
import com.jd.xtl.jdyx.cms.common.util.ParamUtil;
import ${basePackage}.service${m.prefixPoint(table.modelName)}.${table.className}Service;
import ${basePackage}.domain${m.prefixPoint(table.modelName)}.${table.className};
import ${basePackage}.domain${m.prefixPoint(table.modelName)}.${table.className}Vo;
import ${basePackage}.dao${m.prefixPoint(table.modelNames[0])}.${table.className}Mapper;
/**
 * @author ${author!}
 * @since ${timeNow!}
 * ${table.remarks!}
 */
@Slf4j
@Service
public class ${table.className}ServiceImpl implements ${table.className}Service {
    @Resource
    private ${table.className}Mapper ${table.classNameFirstLower}Mapper;

    @Override
    public ${table.className}Vo queryById(Long id) {
        log.info("${table.className}ServiceImpl.queryById,通过id查询,id:{}",id);
        ParamUtil.notNull(id,"id");
        return ${table.classNameFirstLower}Mapper.queryById(id);
    }
    @Override
    public ${table.className}Vo queryForObject(${table.className}Vo query) {
        log.info("${table.className}ServiceImpl.queryForObject,根据条件查询对象,${table.className}Vo:{}",query);
        ParamUtil.notNull(query,"查询条件");
        query.setIsDeleted(YesOrNoFlagEnums.NO.getCode());
        return ${table.classNameFirstLower}Mapper.queryForObject(query);
    }
    @Override
    public List<${table.className+'Vo'}> queryForList(${table.className}Vo query) {
        log.info("${table.className}ServiceImpl.queryForList,查询列表,${table.className}Vo:{}",query);
        if (query == null){
            query = new ${table.className}Vo();
        }
        query.setIsDeleted(YesOrNoFlagEnums.NO.getCode());
        return ${table.classNameFirstLower}Mapper.queryForList(query);
    }
    @Override
    public PageResult<${table.className+'Vo'}> queryPageList(${table.className}Vo query){
        log.info("${table.className}ServiceImpl.queryPageList,查询列表,${table.className}Vo:{}",query);
        if (query == null){
            query = new ${table.className}Vo();
        }
        query.setIsDeleted(YesOrNoFlagEnums.NO.getCode());
        PageHelper.startPage(query.getPageIndex(), query.getPageSize());
        List<${table.className+'Vo'}> result = ${table.classNameFirstLower}Mapper.queryForList(query);
        com.github.pagehelper.PageInfo<${table.className+'Vo'}> pageInfo = new com.github.pagehelper.PageInfo<>(result);

        PageResult<${table.className+'Vo'}> pr = new PageResult<>();
        pr.setPageList(result);
        pr.setPageInfo(new PageInfo(pageInfo.getPageNum(),pageInfo.getPageSize(),(int)pageInfo.getTotal()));
        return pr;
    }
    @Override
    public Long insert(${table.className} entity) {
        log.info("${table.className}ServiceImpl.insert,新增,${table.className}:{}",entity);
        ParamUtil.notNull(entity,"要保存的对象");
        entity.setCreated(new Date());
        entity.setModified(entity.getCreated());
        entity.setIsDeleted(YesOrNoFlagEnums.NO.getCode());
        ${table.classNameFirstLower}Mapper.insert(entity);
        return entity.getId();
    }
    @Override
    public Integer update(${table.className} entity) {
        log.info("${table.className}ServiceImpl.update,更新,${table.className}:{}",entity);
        ParamUtil.notNull(new Object[]{entity,entity.getId()},new String[]{"要更新的对象","id"});
        entity.setModified(new Date());
        return ${table.classNameFirstLower}Mapper.update(entity);
    }
    @Override
    public Integer deleteById(Long id){
        log.info("${table.className}ServiceImpl.deleteById,根据id删除,id:{}",id);
        ParamUtil.notNull(id,"id");
        ${table.className} entity = new ${table.className}();
        entity.setId(id);
        entity.setIsDeleted(YesOrNoFlagEnums.YES.getCode());
        return ${table.classNameFirstLower}Mapper.update(entity);
    }
    @Override
    public Long save(${table.className}Vo param) {
        log.info("${table.className}ServiceImpl.save,保存,param:{}",param);
        ParamUtil.notNull(param,"保存对象");
        this.checkParam(param);

        ${table.className} entity =new ${table.className}();
        BeanUtils.copyProperties(param,entity);
        if(null==param.getId()){
            return this.insert(entity);
        }else{
            Integer updateNum = this.update(entity);
            return Long.valueOf(updateNum);
        }
    }
    private void checkParam(${table.className}Vo param){
        /* TODO 参数验证*/
        if(null==param.getId()){

        }else{

        }
        ${table.className}Vo countQuery =new ${table.className}Vo();
        /* TODO 数据唯一性验证*/
        countQuery.setIdNotEqualTo(param.getId());
        countQuery.setIsDeleted(YesOrNoFlagEnums.NO.getCode());
        Integer num = ${table.classNameFirstLower}Mapper.count(countQuery);
        if(null!=num && num>0){
            throw new BaseException(APIResult.SYS_FAIL,"库中存在相同的数据");
        }
    }
}